#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2011/06/06

'''
import gdata.spreadsheet.service
import re
import string
import sys
from google.appengine.ext import db

stdin = sys.stdin
stdout = sys.stdout
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdin = stdin
sys.stdout = stdout

class UserCommentData(db.Model):
    date = db.StringProperty(required=True, multiline=False)
    userId = db.StringProperty(multiline=False)
    userName = db.StringProperty(multiline=False)
    rating = db.StringProperty(multiline=False)
    comment = db.StringProperty(multiline=False)
    
class UserCommentDetail:
    
    key ='0AgnpWHfnlnQbdDFLQVhPX29ITEgtMXRFb1VJbHhiRUE'
    
    columnHeader = ['date',\
                u'ユーザID',\
                u'ユーザ名',\
                u'評価',\
                u'コメント']
    wksht_id = ''
    row_count = 0

    def outComment(self, keyDate, keyWord, gd_client):
        keyDate = re.sub('"', '', str(keyDate))
        keyWord = re.sub('"', '', str(keyWord))
        row_query = gdata.spreadsheet.service.ListQuery()
#        row_query.start_index = str(2)
#        row_query.max_results = str(1)
        row_query.sq = self.columnHeader[0] + '=' + keyDate
        feed = gd_client.GetListFeed(self.key, self.wksht_id, query=row_query)
        result = []
        for entry in feed.entry:
            if isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed):
#                print '%s %s %s' % (i, entry.title.text, entry.content.text)
                date = entry.custom[self.columnHeader[0]].text
                if re.search(keyDate, date) == None:
                    continue
                # Print this row's value for each column (the custom dictionary is
                # built from the gsx: elements in the entry.) See the description of
                # gsx elements in the protocol guide.
#                searchComment = re.sub("\"", "", str(entry.custom[self.columnHeader[4]].text))
                searchComment = str(entry.custom[self.columnHeader[4]].text)
                if re.search(keyWord, searchComment) != None:
                    result.append('<tr>')
                    result.append('<td>')
                    result.append(entry.custom[self.columnHeader[3]].text)
                    result.append('</td>')
                    result.append('<td>')
                    for comment in searchComment.split(keyWord):
                        result.append(comment)
                        result.append('<span class="highlight">' + keyWord + '</span>')
                    result.pop()
                    result.append('</td>')
                    result.append('</tr>\n')

        return "".join(result)
    
    # udpate database
    def updateComment(self, gd_client, isClear, indexDate):
        if isClear:
            datas = UserCommentData.all()
            for data in datas:
                data.delete()

        row_query = gdata.spreadsheet.service.ListQuery()
        row_query.sq = self.columnHeader[0] + '=' + indexDate
        feed = gd_client.GetListFeed(self.key, self.wksht_id, query=row_query)
            
        for entry in feed.entry:
            if isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed):
#                    print '%s %s %s' % (i, entry.title.text, entry.content.text)
                date = entry.title.text
                # Print this row's value for each column (the custom dictionary is
                # built from the gsx: elements in the entry.) See the description of
                # gsx elements in the protocol guide.
                commentData = UserCommentData(date = date)
#                    userId = entry.custom[self.columnHeader[1]].text
#                    commentData.userId = userId
                userName = entry.custom[self.columnHeader[2]].text
                commentData.userName = userName
                rating = entry.custom[self.columnHeader[3]].text
                commentData.rating = rating
                comment = entry.custom[self.columnHeader[4]].text
                commentData.comment = comment
                commentData.put()

    def __init__(self, id):
        self.wksht_id = id
